import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";
import { formatToken, getToken } from "@/utils/auth";

export type PqbExerciseResult = {
  success: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** 按钮级别权限 */
    permissions: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getPqbExercise = () => {
  const userInfo = getToken();
  return http.request<PqbExerciseResult>(
    "get",
    baseUrlApi("getPqbExercise"),
    {},
    {
      headers: {
        access_token: formatToken(userInfo.accessToken)
      }
    }
  );
};

/** 刷新`token` */
export const getPqbAnswer = () => {
  const userInfo = getToken();
  return http.request<PqbExerciseResult>(
    "get",
    baseUrlApi("getPqbAnswer"),
    {},
    {
      headers: {
        access_token: formatToken(userInfo.accessToken)
      }
    }
  );
};
